package com.cci.kangdao.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;
import com.cci.kangdao.dao.LoginDao;
import com.cci.kangdao.dao.ServiceCarTDao;
import com.cci.kangdao.service.ServiceCarTService;
import com.cci.kangdao.utilTool.AESTool;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.ParameterTool;

import org.json.JSONObject;
//服务车类接口实现类
@Service("ServiceCarTService")
public class ServiceCarTServiceImpl implements ServiceCarTService{

	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private ServiceCarTDao serviceCarTDao;
	
	@Autowired
	private LoginDao loginDao;
	
	//插入服务车信息
	@Transactional
	@Override
	public ModelAndView insertServiceCarT(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			JSONObject obj = new JSONObject();  //返给手机端数据容器
			String userid = AESTool.decryptString(request.getHeader("userid"));//解密userid
			if(StringUtils.isEmpty(MapUtils.getString(map,"StationId",""))) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "服务站id不能为空!");
				ParameterTool.writeResponse(response, obj.toString());	
				return null;
			}
			if(StringUtils.isEmpty(MapUtils.getString(map,"VIN",""))) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "设备号不能为空!");
				ParameterTool.writeResponse(response, obj.toString());	
				return null;
			}
			String  StationId = AESTool.decryptString((String)map.get("StationId"));//解密服务站id
			map.put("StationId", Long.parseLong(StationId));
			map.put("Creator", loginDao.getUserByid(userid).getId());
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = LocalTimeUtil.getRealCurrentTime();  //获取当前时间
			map.put("CreateTime", currentTime);
			//先判断该服务站下有没有相同设备号的服务车
			int count = serviceCarTDao.getServiceCarTByVINStationId(map); 
			if(count > 0) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "已添加此车牌号，不可重复添加!");
			}else {
				serviceCarTDao.insertServiceCarT(map);  //保存服务车信息
				obj.put("data", serviceCarTDao.getServiceCarTListByServiceCarId(map));
				obj.put("status", 0);
				obj.put("msg", "插入成功");
			}
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}

	//修改服务车信息
	@Transactional
	@Override
	public ModelAndView updateServiceCarT(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			JSONObject obj = new JSONObject();   //返给手机端数据容器
			if(StringUtils.isEmpty(MapUtils.getString(map,"StationId",""))) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "服务站id不能为空!");
				ParameterTool.writeResponse(response, obj.toString());	
			}
			if(StringUtils.isEmpty(MapUtils.getString(map,"VIN",""))) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "设备号不能为空!");
				ParameterTool.writeResponse(response, obj.toString());	
			}
			if(StringUtils.isEmpty(MapUtils.getString(map,"ID",""))) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "服务车id不能为空!");
				ParameterTool.writeResponse(response, obj.toString());	
			}
			String userid = AESTool.decryptString(request.getHeader("userid"));//解密userid
			String  StationId = AESTool.decryptString((String)map.get("StationId"));
			map.put("StationId", Long.parseLong(StationId));
			map.put("Updator", loginDao.getUserByid(userid).getId());
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = LocalTimeUtil.getRealCurrentTime();  //获取当前时间
			map.put("UpdateTime", currentTime);
			//先判断该服务站下有没有相同设备号的服务车
			int count = serviceCarTDao.getServiceCarTByVINStationId(map);
			if(count > 0) {
				obj.put("data", "");
				obj.put("status", 1);
				obj.put("msg", "已添加此车牌号，不可重复添加!");
			}else {
				serviceCarTDao.updateServiceCarT(map);
				obj.put("status", 0);
				obj.put("msg", "修改成功");
			}
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}

	//删除服务车信息(修改flag的值即可)
	@Transactional
	@Override
	public ModelAndView deleteServiceCarT(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			JSONObject obj = new JSONObject();   //返给手机端数据容器
			String userid = AESTool.decryptString(request.getHeader("userid"));//解密userid
			map.put("Deleter", loginDao.getUserByid(userid).getId());
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = LocalTimeUtil.getRealCurrentTime();  //获取当前时间
			map.put("DeleteTime", currentTime);
			serviceCarTDao.deleteServiceCarT(map);
			obj.put("data", "");
			obj.put("status", 0);
			obj.put("msg", "删除成功");
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}

	//根据服务站id获取所有的服务车
	@Transactional
	@Override
	public ModelAndView getServiceCarTListByServiceStationId(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		try{
			JSONObject obj = new JSONObject();
			String  StationId = AESTool.decryptString((String)map.get("StationId"));
			map.put("StationId", Long.parseLong(StationId));
			List<Map<String, Object>> serviceCarTList = serviceCarTDao.getServiceCarTListByServiceStationId(map);
			obj.put("status", 0);
			obj.put("msg", "查询成功");
			obj.put("data", new JSONObject().put("serviceCarTList", serviceCarTList));
			ParameterTool.writeResponse(response, obj.toString());	
		} catch(Exception e) {
			ParameterTool.writeErrorResponse(response);	
			//主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			log.error(e.getMessage(),e);
		}
		return null;
	}
}
